package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IAgencyExtDao;
import com.boss.salary.entity.AgencyExt;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;
@Repository
public class AgencyExtDaoImpl extends BaseDaoImpl implements IAgencyExtDao{

    @Override
    public void insertAgencyExt(AgencyExt agencyExt) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TableName.AGENCY_EXT);
        sql.append("(id, biz_key, create_time, update_time, agency_id,")
           .append("agency_address, zip, agency_adm_level_code, adm_dep_type_code, agency_bank_name,")
           .append("agency_bank_cardno, central_agency_code, agency_leader, is_uni_sala, remarks, ")
           .append("has_file, fund_sup_code, capital_type,effective_year, verify_state, is_deleted)")
           .append(" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        Object[] params = new Object[]{
                agencyExt.getId(),agencyExt.getBizKey(),agencyExt.getCreateTime(),agencyExt.getUpdateTime(),
                agencyExt.getAgencyId(), agencyExt.getAgencyAddress(),agencyExt.getZip(),agencyExt.getAgencyAdmLevelCode(),
                agencyExt.getAdmDepTypeCode(), agencyExt.getAgencyBankName(), agencyExt.getAgencyBankCardno(),
                agencyExt.getCentralAgencyCode(),agencyExt.getAgencyLeader(),agencyExt.getIsUniSala(),
                agencyExt.getRemarks(), agencyExt.getHasFile(),agencyExt.getFundSupCode(),agencyExt.getCapitalType(),
                agencyExt.getEffectiveYear(),agencyExt.getVerifyState(), agencyExt.getIsDeleted()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void deleteAgencyExt(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.AGENCY_EXT);
        sql.append(" set is_deleted='1' ")
                .append("where id=?");
        Object[] params = new Object[]{
                id
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updateAgencyExt(AgencyExt agencyExt) {
        String upId = "";
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.AGENCY_EXT);
        sql.append(" set biz_key=?, create_time=?, update_time=?, agency_id=?, agency_address=?,")
           .append("zip=?, agency_adm_level_code=?, adm_dep_type_code=?, agency_bank_name=?, agency_bank_cardno=?,")
           .append("central_agency_code=?, agency_leader=?, is_uni_sala=?, remarks=?, has_file=?,")
           .append("fund_sup_code=?, capital_type=?, effective_year=?, verify_state=?, is_deleted=? ");
            if (!StringUtils.isEmpty(agencyExt.getId())) {
                sql.append(" where id = ?");
                upId = agencyExt.getId();
            } else {
                sql.append(" where agency_id = ?");
                upId = agencyExt.getAgencyId();
            }

        Object[] params = new Object[]{
                agencyExt.getBizKey(),agencyExt.getCreateTime(),agencyExt.getUpdateTime(),agencyExt.getAgencyId(),
                agencyExt.getAgencyAddress(),agencyExt.getZip(),agencyExt.getAgencyAdmLevelCode(),
                agencyExt.getAdmDepTypeCode(),agencyExt.getAgencyBankName(), agencyExt.getAgencyBankCardno(),
                agencyExt.getCentralAgencyCode(),agencyExt.getAgencyLeader(),agencyExt.getIsUniSala(),
                agencyExt.getRemarks(), agencyExt.getHasFile(),agencyExt.getFundSupCode(),agencyExt.getCapitalType(),
                agencyExt.getEffectiveYear(),agencyExt.getVerifyState(), agencyExt.getIsDeleted(), upId
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updateAgencyExtBank(AgencyExt agencyExt) {
        String upId = "";
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.AGENCY_EXT);
        sql.append(" set agency_bank_name=?, update_time = ?, verify_state = ? ");
        if (!StringUtils.isEmpty(agencyExt.getId())) {
            sql.append(" where id = ?");
            upId = agencyExt.getId();
        } else {
            sql.append(" where agency_id = ?");
            upId = agencyExt.getAgencyId();
        }

        Object[] params = new Object[]{
                agencyExt.getAgencyBankName(), agencyExt.getUpdateTime(), agencyExt.getVerifyState(), upId
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public int queryAgencyExt() {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from ").append(TableName.AGENCY_EXT).append(" where is_deleted='0' ");
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class);
    }

    @Override
    public List<AgencyExt> queryAgencyExtPage(PaginationDTO paginationDTO) {
        StringBuilder sql = new StringBuilder();

        sql.append("select  id, biz_key, create_time, update_time, agency_id, ")
                .append("agency_address, zip, agency_adm_level_code, adm_dep_type_code, agency_bank_name,")
                .append("agency_bank_cardno, central_agency_code, agency_leader, is_uni_sala, remarks, ")
                .append("has_file, fund_sup_code, capital_type,effective_year, verify_state, is_deleted  from ")
                .append(TableName.AGENCY_EXT)
                .append(" where is_deleted='0' ");
        sql.append(" order by create_time");
        String querySql = this.boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(null, paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(AgencyExt.class), params);
    }

    @Override
    public void submitVerify(AgencyExt agencyExt) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.AGENCY_EXT);
        sql.append(" set verify_state= ? , update_time = ?")
                .append("where agency_id=?");
        Object[] params = new Object[]{
                SystemDictConstant.IS_SUBMIT, agencyExt.getUpdateTime(), agencyExt.getAgencyId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void revokeVerify(AgencyExt agencyExt) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.AGENCY_EXT);
        sql.append(" set verify_state=? ,  update_time = ?")
                .append("where agency_id=?");
        Object[] params = new Object[]{
                SystemDictConstant.NO_SUBMIT, agencyExt.getUpdateTime(), agencyExt.getAgencyId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }
}
